package com.daye.homework.servlet;

import com.daye.homework.bean.Class;
import com.daye.homework.factory.AdminServiceFactory;
import com.daye.homework.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "RemoveClassServlet" , urlPatterns = "/removeClass")
public class RemoveClassServlet extends HttpServlet {

    private AdminService adminService = AdminServiceFactory.getAdminService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));

        try {
            Class myClass = adminService.getClassById(id);
            if (myClass.getPeopleNum() <= 0){
                //没人了 , 删除
                adminService.deleteClassById(id);
                response.sendRedirect("queryClass");
            }else {
                //有人 , 不给这傻逼删除
                request.setAttribute("message" , "班级还有人,不能删除!");
                request.getRequestDispatcher("classManagerPage.jsp").forward(request , response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
